package com.flong.selenium;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @说明 从网上抓取图片保存到到本地
 * @Version 1.0
 *@Author:liangjilong 
 */
@SuppressWarnings("all")
public class GetImage {
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		//getImages();
		String path1 = "D:/test/img";
		File fromFile = new File(path1);// 源文件夹
		fileRename(fromFile, "Img");
	}
	/**
	 * 将抓取的图片重命名
	 * @param fileDir
	 * @param sequenceCode
	 */
	public static void fileRename(File fileDir, String sequenceCode) {
		File[] files = fileDir.listFiles();
		for (int k = 0; k < files.length; k++) {
			StringBuffer buffer = new StringBuffer(sequenceCode);
			if (files[k].isDirectory()) {
				fileRename(files[k], sequenceCode);
			} else {
				if (k < 10){
					buffer.append("_000").append(k).append(".png");
				}else if (k >= 10 && k < 100){
					buffer.append("_00").append(k).append(".png");
				}else if (k < 1000 && k >= 100){
					buffer.append("_0").append(k).append(".png");
				}else{
					buffer.append("_").append(k).append(".png");
				}
				final int index = files[k].getName().lastIndexOf(".") + 1;
				final String fileType = files[k].getName().substring(index);
				buffer.append(".").append(fileType);
				final String name = buffer.toString();
				int lastIndex=name.lastIndexOf(".");
				String ImgName=name.substring(0, lastIndex);
				final File dirFile = new File(fileDir, ImgName);
				System.out.println("Rename File Path:"+ files[k].getAbsolutePath());
				files[k].renameTo(dirFile);
			}
		}
	}
	/**
	 * 从网上抓取图片
	 */
	private static void getImages(){
		String url="http://liangjilong2014.duapp.com/images/";
		String sequenceCode="__0";
		for (int i = 1; i < 474; i++) {
			StringBuffer buffer = new StringBuffer(sequenceCode);
			if(i<10){
				buffer.append("000").append(i);
			}else if (i >= 10 && i< 100){
				buffer.append("00").append(i);
			}else if (i < 1000 && i >= 100){
				buffer.append("0").append(i);
			} else{
				buffer.append("").append(i);
				
			}
			String URL = url+buffer.toString()+".png";
			byte[] btImg = getImageFromNetByUrl(URL);
			if(null != btImg && btImg.length > 0){
				System.out.println("读取到：" + btImg.length + " 字节");
				String fileName = "background.png";
				writeToDisk(btImg, fileName);
			}else{
				System.out.println("没有从该连接获得内容");
			}
		}
	}
	/**
	 * 将图片写入到磁盘
	 * @param img 图片数据流
	 * @param fileName 文件保存时的名称	
	 */
	private static void writeToDisk(byte[] img, String ...fileName){
		try {
			File file = new File("d:/test/" + fileName);
			FileOutputStream fops = new FileOutputStream(file);
			fops.write(img);
			fops.flush();
			fops.close();
			System.out.println("图片已经写入到D盘");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 根据地址获得数据的字节流
	 * @param strUrl 网络连接地址
	 * @return
	 */
	private static byte[] getImageFromNetByUrl(String strUrl){
		try {
			URL url = new URL(strUrl);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5 * 1000);
			InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
			byte[] btImg = readStream(inStream);//得到图片的二进制数据
			return btImg;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 从输入流中获取数据
	 * @param inStream 输入流
	 * @return
	 * @throws Exception
	 */
	private static byte[] readStream(InputStream inStream) throws Exception{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len=inStream.read(buffer)) != -1 ){
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}
}